package com.bishe.o2oo.service.impl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bishe.o2oo.cache.JedisUtil;
import com.bishe.o2oo.dao.AreaDao;
import com.bishe.o2oo.entity.Area;
import com.bishe.o2oo.exceptions.AreaOperationException;
import com.bishe.o2oo.service.AreaService;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;


@Service
public class AreaServiceImpl implements AreaService{//托管在spring容器中，自动注入
	@Autowired
	private AreaDao areaDao;
	@Autowired
	private JedisUtil.Keys jedisKeys;
	@Autowired
	private JedisUtil.Strings jedisStrings;
	
	@Override
	public List<Area> getAreaList() {
		// 定义redis的key
				String key = AREALISTKEY;
				// 定义接收对象
				List<Area> areaList = null;
				// 定义jackson数据转换操作类
				ObjectMapper mapper = new ObjectMapper();
				// 判断key是否存在
				if (!jedisKeys.exists(key)) {
					// 若不存在，则从数据库里面取出相应数据
					areaList = areaDao.queryArea();
					// 将相关的实体类集合转换成string,存入redis里面对应的key中
					String jsonString;
					try {
						jsonString = mapper.writeValueAsString(areaList);
					} catch (JsonProcessingException e) {
						e.printStackTrace();
						
						throw new AreaOperationException(e.getMessage());
					}
					jedisStrings.set(key, jsonString);
				}else{
					// 若存在，则直接从redis里面取出相应数据
					String jsonString = jedisStrings.get(key);
					// 指定要将string转换成的集合类型
					JavaType javaType = mapper.getTypeFactory().constructParametricType(ArrayList.class, Area.class);
					try {
						// 将相关key对应的value里的的string转换成对象的实体类集合
						areaList = mapper.readValue(jsonString, javaType);
					} catch (JsonParseException e) {
						e.printStackTrace();
						//logger.error(e.getMessage());
						throw new AreaOperationException(e.getMessage());
					} catch (JsonMappingException e) {
						e.printStackTrace();
						//logger.error(e.getMessage());
						throw new AreaOperationException(e.getMessage());
					} catch (IOException e) {
						e.printStackTrace();
						//logger.error(e.getMessage());
						throw new AreaOperationException(e.getMessage());
					}
				}
				return areaList;
	}

	

	
}
